package api

import (
	"admin100/app/model"
	"admin100/app/model/gamemodel"
	"admin100/app/service"
	"admin100/app/service/centerservice"
	"admin100/app/service/commonservice"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/response"
	"admin100/library/utils"
	"encoding/json"
	"strings"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

var ToolMgr = &toolMgrApi{}

type toolMgrApi struct{}

// @summary 获取IP信息
// @tags 工具
// @produce json
// @param   ip formData  string true "ip"
// @router  /tool/get-ip-origin [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *toolMgrApi) GetIpOrigin(r *ghttp.Request) {
	var req struct {
		Ip string `v:"required|ip#ip不能为空|ip非法"`
	}
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	response.JsonSuc(r, service.Tool.GetIpLocation(req.Ip))
}

// @summary 玩家查询
// @tags 工具
// @produce json
// @param   platformId formData  string true "平台id"
// @param   serverId formData  string true "服务号id"
// @param   channelList formData  string true "渠道列表,如： all, local_test"
// @param   ip formData  string false "ip"
// @param   isOnline formData  string false "是否在线"
// @param   nickName formData  string false "昵称"
// @param   playerId formData  string false "角色ID"
// @param   startTime formData  string false "开始时间"
// @param   endTime formData  string false "结束时间"
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /tool/player-list [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *toolMgrApi) PlayerList(r *ghttp.Request) {
	var req gamemodel.PlayerListReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	sNode := centerservice.CGameServer.FetchNodeStrByPtIdAndSid(req.PlatformId, req.ServerId)
	data, total := commonservice.GamePlayer.GetPlayerList(sNode, &req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 发送公告
// @tags 工具
// @produce json
// @param   id formData  string false "公告ID"
// @param   platformId formData  string true "平台id"
// @param   ServerIdList formData  string true "区服ID列表"
// @param   IsAllServer formData  string true "是否全服发送"
// @param   Content formData  string false "公告内容"
// @param   NoticeType formData  string false "类型[0:立即发送 1:定时发送 2:循环发送]"
// @param   NoticeTime formData  string false "发送次数"
// @param   CronTimeStr formData  string false "定时发送时间"
// @param   CronTimes formData  string false "定时发送次数"
// @router  /tool/send-cron-notice [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *toolMgrApi) SendCronNotice(r *ghttp.Request) {
	var req model.SendNoticeReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	_ = service.NoticeLog.CreateNoticeLog(&req, service.Context.GetUser(r.Context()))
	response.JsonSuc(r)
}

// @summary 定时公告列表
// @tags 工具
// @produce json
// @param   platformId formData  string true "平台id"
// @param   NoticeType formData  string false "类型[0:立即发送 1:定时发送 2:循环发送]"
// @param   isShowFinish formData  string false "只显示已完成"
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /tool/notice-log [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *toolMgrApi) NoticeLog(r *ghttp.Request) {
	var req model.NoticeLogReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	data, total := service.NoticeLog.GetNoticeLogList(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary  关闭定时公告
// @tags 工具
// @produce json
// @param   ids formData  string true "公告id列表, 如 12,31"
// @router  /tool/close-cron-notice [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *toolMgrApi) CloseCronNotice(r *ghttp.Request) {
	var req model.DelNoticeLogReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	u := service.Context.GetUser(r.Context())
	service.NoticeLog.CloseCronNotice(u.Id, strings.Split(req.Ids, ","))
	response.JsonSuc(r)
}

// @summary 删除公告
// @tags 工具
// @produce json
// @param   ids formData  string true "公告id列表, 如 12,31"
// @router  /tool/del-notice-log [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *toolMgrApi) DelNoticeLog(r *ghttp.Request) {
	var req model.DelNoticeLogReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	service.NoticeLog.DelNoticeLog(strings.Split(req.Ids, ","))
	response.JsonSuc(r)
}

// @summary 封禁设置
// @tags 工具
// @produce json
// @param   platformId formData  string true "平台id"
// @param   serverId formData  string true "区服ID"
// @param   playerId formData  int true "角色id"
// @param   accId formData  string false "帐号名"
// @param   type formData  int false "禁封类型"
// @param   sec formData  int false "时间"
// @param   range formData  int false "是否循环"
// @router  /tool/set-forbid [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *toolMgrApi) SetForbid(r *ghttp.Request) {
	var req model.SetForbidReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	sNode := centerservice.CGameServer.FetchNodeStrByPtIdAndSid(req.PlatformId, req.ServerId)

	// gameplayer := gamedao.NewPlayerDao(sNode)
	// gameplayer.Get

	one := commonservice.GamePlayer.GetPlayerOne(sNode, req.PlatformId, req.ServerId, req.PlayerId)
	if one == nil {
		defErr.Panic(define.RecordNodFound)
	}

	req.AccId = one.AccId
	data, err := json.Marshal(req)
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}
	url := utils.GetCenterURL() + "/set_disable"
	if s := utils.HttpRequest(url, string(data)); s == "fail" {
		response.JsonFail(r)
	} else {
		response.JsonSuc(r)
	}
}

// @summary 封禁列表
// @tags 工具
// @produce json
// @param   platformId formData  string true "平台id"
// @param   ServerId formData  string true "区服ID"
// @param   PlayerName formData  string true "角色名"
// @router  /tool/forbid-log [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *toolMgrApi) ForbidLog(r *ghttp.Request) {
	var req model.ForbidLogListReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	if req.PlayerName != "" {
		serverId, _ := commonservice.GamePlayer.SplitPlayerName(req.PlayerName)
		sNode := centerservice.CGameServer.FetchNodeStrByPtIdAndSid(req.PlatformId, serverId)
		player := commonservice.GamePlayer.GetPlayerByPlatformIdAndNickname(sNode, req.PlayerName)
		req.PlayerId = int(player.Id)
	}

	data, total := service.Tool.GetForbidLogList(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 后台充值日志
// @tags 工具
// @produce json
// @param   platformId formData  string true "平台id"
// @param   ServerId formData  string false "区服ID"
// @param   PlayerName formData  string false "角色名"
// @param   startTime formData  string false "开始时间"
// @param   endTime formData  string false "结束时间"
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /tool/background-charge-list [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *toolMgrApi) BackgroundChargeList(r *ghttp.Request) {
	var req model.BackgroundChargeLogReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	if req.PlayerName != "" {
		serverId, _ := commonservice.GamePlayer.SplitPlayerName(req.PlayerName)
		sNode := centerservice.CGameServer.FetchNodeStrByPtIdAndSid(req.PlatformId, serverId)
		player := commonservice.GamePlayer.GetPlayerByPlatformIdAndNickname(sNode, req.PlayerName)
		req.PlayerId = int(player.Id)
	}
	data, total := service.Tool.GetBackgroundChargeLogList(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 充值
// @tags 工具
// @produce json
// @param   platformId formData  string true "平台id"
// @param   ServerId formData  string true "区服ID"
// @param   PlayerId formData  string true "角色ID"
// @param   ChargeValue formData  string false "充值数量"
// @param   ChargeType formData  string false "充值类型"
// @router  /tool/background-recharge [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *toolMgrApi) BackgroundRecharge(r *ghttp.Request) {
	var req model.RechargeReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	sNode := centerservice.CGameServer.FetchNodeStrByPtIdAndSid(req.PlatformId, req.ServerId)
	player := commonservice.GamePlayer.GetPlayerOne(sNode, req.PlatformId, req.ServerId, req.PlayerId)
	curUser := service.Context.GetUser(r.Context())

	service.Tool.BackgrounRecharge(sNode, &req, player, curUser)

	response.JsonSuc(r)
}

// @summary 内部帐号管理_设置帐号类型
// @tags 工具
// @produce json
// @param   platformId formData  string true "平台id"
// @param   ServerId formData  string true "区服ID"
// @param   playerId formData  string true "角色ID"
// @param   type formData  int true "类型"
// @param   payTimes formData  int false "支付次数"
// @router  /tool/set-account-type [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *toolMgrApi) SetAccountType(r *ghttp.Request) {
	var req gamemodel.SetAccountTypeReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	request, err := json.Marshal(req)
	if err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	node := centerservice.CGameServer.FetchNodeStrByPtIdAndSid(req.PlatformId, req.ServerId)
	url := centerservice.CGameNode.GetGameURLByNode(node)
	utils.HttpRequest(url, string(request))

	response.JsonSuc(r)
}

// @summary 邮件列表
// @tags 工具
// @produce json
// @param   platformId formData  string true "平台id"
// @param   startTime formData  string false "开始时间"
// @param   endTime formData  string false "结束时间"
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /tool/mail-log [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *toolMgrApi) MailLog(r *ghttp.Request) {
	var req model.MailLogReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	data, total := service.Tool.MailLog(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 发送邮件
// @tags 工具
// @produce json
// @param   platformId formData  string true "平台id"
// @param   serverIdList formData  string false "区服ID列表, s153,s160"
// @param   playerNameList formData  string false "角色名列表, s153.Bansarian"
// @param   title formData  int true "标题"
// @param   content formData  int true "内容"
// @param   mailItemList formData  string false "附件,如 [{'propType':1,'propId':2,'propNum':2},{'propType':2,'propId':101,'propNum':2}]"
// @param   type formData  int true "类型"
// @param   conditionsId formData  int false "条件ID"
// @param   conditionsValue formData  int false "条件内容"
// @router  /tool/send-mail [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *toolMgrApi) SendMail(r *ghttp.Request) {
	var req gamemodel.SendMailReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	//参数处理
	if !utils.InStrSlice(req.Type, []string{"1", "2", "3"}) {
		defErr.Panic(define.EmailTypeErr)
	}

	var mailItemList = make([]gamemodel.Prop, 0)

	for _, v := range req.MailItemList {
		propObjs := map[string]int{}
		if err := json.Unmarshal([]byte(v), &propObjs); err != nil {
			defErr.Panic(define.Fail)
		}

		var prop = gamemodel.Prop{}
		if err := gconv.Struct(propObjs, &prop); err != nil {
			defErr.Panic(define.Fail, err.Error())
		}
		mailItemList = append(mailItemList, prop)
	}
	var httpReq = gamemodel.SendMailHttpRequest{
		PlatformId:      req.PlatformId,
		NodeList:        strings.Split(req.NodeList, ","),
		PlayerNameList:  req.PlayerNameList,
		MailItemList:    mailItemList,
		Title:           req.Title,
		Content:         req.Content,
		Type:            req.Type,
		ConditionsId:    req.ConditionsId,
		ConditionsValue: req.ConditionsValue,
	}
	curUser := service.Context.GetUser(r.Context())
	service.Tool.SendMail(httpReq, curUser.Id)
	response.JsonSuc(r)
}
